#include "Compare_Operation.h"


Compare_Operation::Compare_Operation(void)
{
	this->size_limit = 0;
	this->time_limit = 0;
	this->compare_base = true;

	result=new char[1000];
	strcpy(result,"ldapsearch ");
}



Compare_Operation::~Compare_Operation(void)
{
}

Compare_Operation::Compare_Operation(char *dn,int auth_type,char **option,int nr_opt,char *ldap_URI,vector<Atribut<char*>> filtru)
	:LDAP_Operation(dn,auth_type,ldap_URI,option,nr_opt)
{
	
	this->size_limit = 0;
	this->time_limit = 0;
	this->compare_base = true;

	result=new char[1000];
	strcpy(result,"ldapsearch ");

	compare_filter = filtru;
}

LDAP_Operation* Compare_Operation::operate()
{


	switch(this->auth_type)
	{
	case 0: strcat(result,"-x ");
		break;
	case 1: strcat(result," ");
		break;
	default: printf("\nExceptie:");
		//exceptie;
	}

	for(int i=0;i<nr_opt;i++)
	{
		strcat(result,option[i]);
		strcat(result," ");
	}


	if(strlen(Ldap_URI))
	{
		strcat(result,"-H ");
		strcat(result,Ldap_URI);
		strcat(result," ");
	}

	if(this->compare_base)
		strcat(result,"-b ");

	//adaugarea dn-ului

	if(strlen(this->DN))
		strcat(result,this->DN);
	strcat(result," ");

	// adaugarea atributelor-filtrelor

	
	for(int i=0;i<compare_filter.size();i++)
	{
		strcat(result,compare_filter[i].daNume());
		
		if(!strcmp(compare_filter[i].daNume(),"dn") && !strcmp(compare_filter[i].daAtribut(),""))
			strcat(result,":");
			
		else
			strcat(result,"=");

		if(strcmp(compare_filter[i].daAtribut(),""))
			strcat(result,compare_filter[i].daAtribut());
		strcat(result,",");
	}

	if(compare_filter.size())
		result[strlen(result)-1]=' ';

	strcat(result,"*");

	LDAP_Operation *op=new Compare_Operation((*this));
	//!!!!!!!! SMART POINTERI PT A LUCRA CU CEEA CE RETURNEZI
	return op;
}
